package com.midrange.rse_enhancements.chgmsg;

/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.rse.ui.dialogs.SystemPromptDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.ibm.etools.iseries.services.qsys.api.IQSYSMessageDescription;

/**
 * Edit message dialog.
 * 
 * @author David Gibbs
 *
 */

public class EditMessageTextDialog extends SystemPromptDialog {

	private static final int MAX_LEVEL_1_LENGTH = 132;
	private static final int MAX_LEVEL_2_LENGTH = 3000;
	
	private String messageHelp = "";;

	private SashForm sashForm = null;
	private Composite sashTop = null;
	private Composite sashbottom = null;
	private Text unformattedText = null;
	private Text formattedTextLabel = null;

	private Composite panel = null;

	private String messageText;
	private Label usedValue;
	private Label remainingValue;

	private static final Display DISPLAY = Display.getDefault();
	
	public EditMessageTextDialog(Shell shell, IQSYSMessageDescription msg) {
		super(shell, MessageFormat.format("Edit Message Help - {0}",msg.getID()));
		messageHelp = msg.getHelp();
		messageText = msg.getText();
	}


	@Override
	protected Control createInner(Composite parent) {

		panel = new Composite(parent, SWT.NONE);
		initialize();

		unformattedText.setText(messageHelp);
		refreshMessageText();

		return panel;
	}

	@Override
	protected Control getInitialFocusControl() {
		return unformattedText;
	}


	public String getMessageText() {
		return messageHelp;
	}

	/**
	 * This method initializes sashForm	
	 *
	 */
	private void createSashForm() {
		GridData sashLayoutData = new GridData();
		sashLayoutData.grabExcessHorizontalSpace = true;
		sashLayoutData.horizontalAlignment = GridData.FILL;
		sashLayoutData.verticalAlignment = GridData.FILL;
		sashLayoutData.grabExcessVerticalSpace = true;
		sashLayoutData.grabExcessHorizontalSpace = true;

		sashForm = new SashForm(panel, SWT.BORDER | SWT.V_SCROLL);
		sashForm.setOrientation(SWT.VERTICAL);
		sashForm.setLayoutData(sashLayoutData);
		createSashTop();
		createSashbottom();
	}

	private Font getBoldFont() {
		
		Font f = JFaceResources.getDialogFont();
		
		FontData fontData = f.getFontData()[0];
		fontData.setStyle(SWT.BOLD);
		
		Font boldFont = new Font(DISPLAY, fontData);
		
		return boldFont;
	}

	/**
	 * This method initializes sashTop	
	 *
	 */
	private void createSashTop() {

		sashTop = new Composite(sashForm, SWT.NONE);
		sashTop.setLayout(new GridLayout());

		Label messageTextLabel = new Label(sashTop, SWT.BORDER);
		messageTextLabel.setText(MessageFormatter.formatForHumans(messageText));
		messageTextLabel.setFont(getBoldFont());
		messageTextLabel.setToolTipText("Current first level message text");
		
		GridData messageTextLabelLayoutData = new GridData();
		messageTextLabelLayoutData.grabExcessHorizontalSpace = true;
		messageTextLabelLayoutData.horizontalAlignment = GridData.FILL;
		
		messageTextLabel.setLayoutData(messageTextLabelLayoutData);
		
		formattedTextLabel = new Text(sashTop, SWT.V_SCROLL | SWT.BORDER);
		formattedTextLabel.setText("");
		formattedTextLabel.setEditable(false);

		formattedTextLabel.setFont(new Font(DISPLAY, 
				"Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$

		GridData formattedTextLayoutData = new GridData();
		formattedTextLayoutData.grabExcessHorizontalSpace = true;
		formattedTextLayoutData.horizontalAlignment = GridData.FILL;
		formattedTextLayoutData.verticalAlignment = GridData.FILL;
		formattedTextLayoutData.grabExcessVerticalSpace = true;
		formattedTextLayoutData.minimumHeight = 100;
		
		formattedTextLabel.setLayoutData(formattedTextLayoutData);

	}

	
	/**
	 * This method initializes sashbottom	
	 *
	 */
	private void createSashbottom() {
		GridData unformattedTextLayoutData = new GridData();
		unformattedTextLayoutData.grabExcessHorizontalSpace = true;
		unformattedTextLayoutData.horizontalAlignment = GridData.FILL;
		unformattedTextLayoutData.verticalAlignment = GridData.FILL;
		unformattedTextLayoutData.grabExcessVerticalSpace = true;
		unformattedTextLayoutData.widthHint = 500;
		unformattedTextLayoutData.minimumHeight = 100;

		sashbottom = new Composite(sashForm, SWT.NONE);
		sashbottom.setLayout(new GridLayout());

		unformattedText = new Text(sashbottom, 
				SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);

		unformattedText.setLayoutData(unformattedTextLayoutData);
		unformattedText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				refreshMessageText();
			}
		});
		
		createCharacterCountContainer();
	}

	private void createCharacterCountContainer() {
		RowLayout characterCountLayout = new RowLayout();
		characterCountLayout.fill = true;
		characterCountLayout.justify = false;
		GridData characterCountLayoutData = new GridData();
		characterCountLayoutData.grabExcessHorizontalSpace = true;
		characterCountLayoutData.horizontalAlignment = GridData.FILL;
		Composite characterCountContainer = new Composite(sashbottom, SWT.NONE);
		Label charactersLabel = new Label(characterCountContainer, SWT.NONE);
		charactersLabel.setText("Characters:");

		charactersLabel.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD)); //$NON-NLS-1$
		RowLayout usedLayout = new RowLayout();
		usedLayout.marginBottom = 0;
		usedLayout.marginRight = 3;
		usedLayout.marginTop = 0;
		usedLayout.pack = false;
		usedLayout.marginLeft = 3;
		Composite usedContainer = new Composite(characterCountContainer, SWT.NONE);
		usedContainer.setLayout(usedLayout);
		Label usedLabel = new Label(usedContainer, SWT.NONE);
		usedLabel.setText("Used:");
		usedValue = new Label(usedContainer, SWT.NONE);

		characterCountContainer.setLayoutData(characterCountLayoutData);
		characterCountContainer.setLayout(characterCountLayout);

		RowLayout remainingLayout = new RowLayout();
		remainingLayout.marginBottom = 0;
		remainingLayout.pack = false;
		remainingLayout.marginTop = 0;
		Composite remainingContainer = new Composite(characterCountContainer, SWT.NONE);
		remainingContainer.setLayout(remainingLayout);
		Label remainingLabel = new Label(remainingContainer, SWT.NONE);
		remainingLabel.setText("Remaining:");
		remainingValue = new Label(remainingContainer, SWT.NONE);
	}
	
	private void refreshMessageText() {

		messageHelp = unformattedText.getText();

		String formattedText = MessageFormatter.format(messageHelp);

		formattedTextLabel.setText(formattedText);

		int charactersUsed = textLength(messageHelp);
		int charactersRemaining = MAX_LEVEL_2_LENGTH - charactersUsed;
		
		usedValue.setText(Integer.toString(charactersUsed));
		remainingValue.setText(Integer.toString(charactersRemaining));
		
		formattedTextLabel.update();

	}

	private int textLength(String text) {
		boolean doubleWidthMode = false;
		int count = 0;
		
		for (int p=0;p<text.length();p++) {
			char c = text.charAt(p);
			boolean multiByte = isMultiByte(c);
			
			if (multiByte && !doubleWidthMode) {
				count++;
				doubleWidthMode = true;
			} else if (!multiByte && doubleWidthMode) {
				count++;
				doubleWidthMode = false;
			}
			
			count += (multiByte ? 2 : 1);
		}
		
		return count;
	}
	
	private boolean isMultiByte(char c) {
		boolean multiByte = false;
		
		try {
			String s = Character.toString(c);
			byte[] byteArr = s.getBytes("UTF-8"); //$NON-NLS-1$
			multiByte = (byteArr.length == 1);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return multiByte;
	}

	    
	private void initialize() {

		createSashForm();
		panel.setLayout(new GridLayout());

		GridData masterLayoutData = new GridData();
		masterLayoutData.grabExcessHorizontalSpace = true;
		masterLayoutData.horizontalAlignment = GridData.FILL;
		masterLayoutData.verticalAlignment = GridData.FILL;
		masterLayoutData.grabExcessVerticalSpace = true;
		panel.setLayoutData(masterLayoutData);

	}


}
